package siga.visao_academico;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import siga.banco.conexao;
import siga.navegadores.navegador;
import siga.visao_busca.buscaCurso;
import siga.visao_busca.buscaVer;
import siga.visao.index;
import siga.visao.verErros;

/**
 *
 * @author rafaelfrasson
 */
public class Atividade_Complementar_Tela extends javax.swing.JInternalFrame {

    Object[] JOopcoes = {"Sim","Não"};
    /** Creates new form Atividade_Complementar_Tela */
    public Atividade_Complementar_Tela() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtfCod_acad = new javax.swing.JTextField();
        jtfDesc_acad = new javax.swing.JTextField();
        jbNovo = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtfDescricao = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jtfDt_ini = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        jtfDt_fim = new javax.swing.JFormattedTextField();
        jLabel5 = new javax.swing.JLabel();
        jtfEmpresa = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jtfCrg_hr = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jbAlterar = new javax.swing.JButton();
        jbCancelar = new javax.swing.JButton();
        jbSalvar = new javax.swing.JButton();
        jbExcluir = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        jButton2 = new javax.swing.JButton();
        jbPrint = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jtfTotal = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Atividade Complementar");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/sobre_16x16.png"))); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText("Acadêmico:");
        jLabel1.setName("jLabel1"); // NOI18N

        jtfCod_acad.setEnabled(false);
        jtfCod_acad.setName("jtfCod_acad"); // NOI18N

        jtfDesc_acad.setEnabled(false);
        jtfDesc_acad.setName("jtfDesc_acad"); // NOI18N

        jbNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/novo_32x32.png"))); // NOI18N
        jbNovo.setText("Novo");
        jbNovo.setEnabled(false);
        jbNovo.setName("jbNovo"); // NOI18N
        jbNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbNovoActionPerformed(evt);
            }
        });

        jLabel2.setText("Curso / Palestra:");
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtfDescricao.setColumns(20);
        jtfDescricao.setEditable(false);
        jtfDescricao.setRows(5);
        jtfDescricao.setName("jtfDescricao"); // NOI18N
        jScrollPane1.setViewportView(jtfDescricao);

        jLabel3.setText("Data de Início:");
        jLabel3.setName("jLabel3"); // NOI18N

        jtfDt_ini.setEditable(false);
        try {
            jtfDt_ini.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jtfDt_ini.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jtfDt_ini.setName("jtfDt_ini"); // NOI18N

        jLabel4.setText("Data de Fim:");
        jLabel4.setName("jLabel4"); // NOI18N

        jtfDt_fim.setEditable(false);
        try {
            jtfDt_fim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jtfDt_fim.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jtfDt_fim.setName("jtfDt_fim"); // NOI18N

        jLabel5.setText("Empresa:");
        jLabel5.setName("jLabel5"); // NOI18N

        jtfEmpresa.setEditable(false);
        jtfEmpresa.setName("jtfEmpresa"); // NOI18N

        jLabel6.setText("Carga Horária:");
        jLabel6.setName("jLabel6"); // NOI18N

        jtfCrg_hr.setEditable(false);
        jtfCrg_hr.setName("jtfCrg_hr"); // NOI18N

        jLabel7.setText("Horas");
        jLabel7.setName("jLabel7"); // NOI18N

        jbAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/editar_32x32.png"))); // NOI18N
        jbAlterar.setText("Alterar");
        jbAlterar.setEnabled(false);
        jbAlterar.setName("jbAlterar"); // NOI18N
        jbAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAlterarActionPerformed(evt);
            }
        });

        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/cancelar_32x32.png"))); // NOI18N
        jbCancelar.setText("Cancelar");
        jbCancelar.setEnabled(false);
        jbCancelar.setName("jbCancelar"); // NOI18N
        jbCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelarActionPerformed(evt);
            }
        });

        jbSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/salvar_32x32.png"))); // NOI18N
        jbSalvar.setText("Salvar");
        jbSalvar.setEnabled(false);
        jbSalvar.setName("jbSalvar"); // NOI18N
        jbSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalvarActionPerformed(evt);
            }
        });

        jbExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/remover_32x32.png"))); // NOI18N
        jbExcluir.setText("Remover");
        jbExcluir.setEnabled(false);
        jbExcluir.setName("jbExcluir"); // NOI18N
        jbExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbExcluirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jbNovo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbAlterar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbSalvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbExcluir))
                    .addComponent(jLabel2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jtfDt_ini, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfDt_fim)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jtfEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 497, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfCrg_hr)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7))
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jtfCod_acad, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfDesc_acad))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 850, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCod_acad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDesc_acad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbNovo)
                    .addComponent(jbAlterar)
                    .addComponent(jbCancelar)
                    .addComponent(jbSalvar)
                    .addComponent(jbExcluir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfDt_ini, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfDt_fim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jtfEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfCrg_hr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/buscar_32x32.png"))); // NOI18N
        jButton2.setText("Acadêmico");
        jButton2.setToolTipText("Busca somente Acadêmicos");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jbPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/relatorio_32x32.png"))); // NOI18N
        jbPrint.setText("Impressão");
        jbPrint.setEnabled(false);
        jbPrint.setFocusable(false);
        jbPrint.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jbPrint.setName("jbPrint"); // NOI18N
        jbPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbPrintActionPerformed(evt);
            }
        });
        jToolBar1.add(jbPrint);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Curso / Palestra", "Empresa", "Carga Hr."
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(70);
        jTable1.getColumnModel().getColumn(0).setMaxWidth(70);
        jTable1.getColumnModel().getColumn(3).setPreferredWidth(80);
        jTable1.getColumnModel().getColumn(3).setMaxWidth(80);

        jPanel2.setName("jPanel2"); // NOI18N

        jtfTotal.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jtfTotal.setForeground(new java.awt.Color(180, 22, 22));
        jtfTotal.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jtfTotal.setText("0");
        jtfTotal.setName("jtfTotal"); // NOI18N

        jLabel8.setText("Carga Horária Total:");
        jLabel8.setName("jLabel8"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(695, Short.MAX_VALUE)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfTotal)
                    .addComponent(jLabel8))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 906, Short.MAX_VALUE)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 906, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 906, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        buscaVer bv = new buscaVer(null,true);
        bv.setTipo("Unimestre");
        bv.setLocation((index.tela.width-bv.getWidth())/2, (index.tela.height-bv.getHeight())/2);
        bv.setVisible(true);
        if(buscaVer.isConfirmaBusca()){
            jtfCod_acad.setText(index.LRpessoa.get(0).toString());
            jtfDesc_acad.setText(index.LRpessoa.get(1).toString());
            carrega_tabela();
            jbNovo.setEnabled(true);
            jbPrint.setEnabled(true);
        }
        index.LRpessoa.add(0, "");
        index.LRpessoa.add(1, "");
}//GEN-LAST:event_jButton2ActionPerformed

    private void jbPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbPrintActionPerformed
        index.LRcurso.clear();
        buscaCurso bc = new buscaCurso(null,true);
        bc.setLocation((index.tela.width-bc.getWidth())/2, (index.tela.height-bc.getHeight())/2);
        bc.setVisible(true);
        if(buscaCurso.isConfirmaCurso()){
            if(!jtfCod_acad.getText().equals("")){
                navegador.URL(index.relatorio+"relatorios/relatorio_atividade_complementar.php?cod_emp="+index.cod_emp+"&curso="+index.LRcurso.get(0)+"&cod_pessoa="+jtfCod_acad.getText()+"&desc_pessoa="+jtfDesc_acad.getText());
            }else{
                JOptionPane.showMessageDialog(null, "Selecione um Acadêmico.","Informação do Sistema",JOptionPane.INFORMATION_MESSAGE);
            }
            index.LRcurso.clear();
        }
}//GEN-LAST:event_jbPrintActionPerformed

    private void jbNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbNovoActionPerformed
        clear_campos();
        campos();
        jTable1.setEnabled(false);
        jbCancelar.setEnabled(true);
        jbSalvar.setEnabled(true);
    }//GEN-LAST:event_jbNovoActionPerformed

    private void jbSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalvarActionPerformed
        if(!jtfDescricao.getText().equals("") && !jtfDt_fim.getText().equals("  /  /    ") && !jtfDt_ini.getText().equals("  /  /    ") && !jtfEmpresa.getText().equals("") && !jtfCrg_hr.getText().equals("")){
            if(jbNovo.isEnabled()){
                try {
                    String dt_ini = new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("dd/MM/yyyy").parse(jtfDt_ini.getText()));
                    String dt_fim = new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("dd/MM/yyyy").parse(jtfDt_fim.getText()));
                    conexao.Inserir("insert into atividade_complementar (cod_emp,descricao,dt_inicio_ativ_comp, dt_fim_ativ_comp,nm_emp_ativ_comp,cg_hr_ativ_comp,cod_aluno,nm_aluno) " + 
                            " values (" + index.cod_emp + ",\"" + jtfDescricao.getText() + "\",\"" + dt_ini + "\",\"" + dt_fim + "\",\"" + jtfEmpresa.getText() + "\"," + jtfCrg_hr.getText() + "," + jtfCod_acad.getText() + ", \"" + jtfDesc_acad.getText() + "\")");
                } catch (ParseException ex) {
                    verErros.setErro(ex.getMessage());
                }
            }else{
                try {
                    String dt_ini = new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("dd/MM/yyyy").parse(jtfDt_ini.getText()));
                    String dt_fim = new SimpleDateFormat("yyyy-MM-dd").format(new SimpleDateFormat("dd/MM/yyyy").parse(jtfDt_fim.getText()));
                    conexao.Inserir("replace into atividade_complementar (codigo,cod_emp,descricao,dt_inicio_ativ_comp, dt_fim_ativ_comp,nm_emp_ativ_comp,cg_hr_ativ_comp,cod_aluno,nm_aluno) " +
                            " values ("+ jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString() +"," + index.cod_emp + ",\"" + jtfDescricao.getText() + "\",\"" + dt_ini + "\",\"" + dt_fim + "\",\"" + jtfEmpresa.getText() + "\"," + jtfCrg_hr.getText() + "," + jtfCod_acad.getText() + ", \"" + jtfDesc_acad.getText() + "\")");
                } catch (ParseException ex) {
                    verErros.setErro(ex.getMessage());
                }
            }
            jbSalvar.setEnabled(false);
            jbCancelar.setEnabled(false);
            jbNovo.setEnabled(true);
            jbAlterar.setEnabled(false);
            jTable1.setEnabled(true);
            carrega_tabela();
            campos();
            clear_campos();
        }else{
            JOptionPane.showMessageDialog(null, "Todos os campos devem estar preenchidos.", "Informação do Sistema",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbSalvarActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount() == 2){
            try {
                if(jbAlterar.isEnabled() || jbNovo.isEnabled()){
                    conexao.Query("select * from atividade_complementar where cod_aluno=" + jtfCod_acad.getText() + " and codigo=" + jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString() + " and cod_emp=" + index.cod_emp + " order by descricao");
                    conexao.rs.next();
                    jtfDescricao.setText(conexao.rs.getString("descricao"));
                    jtfCrg_hr.setText(conexao.rs.getString("cg_hr_ativ_comp"));
                    jtfEmpresa.setText(conexao.rs.getString("nm_emp_ativ_comp"));
                    jtfDt_fim.setText(new SimpleDateFormat("dd/MM/yyyy").format(conexao.rs.getDate("dt_fim_ativ_comp")));
                    jtfDt_ini.setText(new SimpleDateFormat("dd/MM/yyyy").format(conexao.rs.getDate("dt_inicio_ativ_comp")));

                    jbNovo.setEnabled(false);
                    jbAlterar.setEnabled(true);
                    jbExcluir.setEnabled(true);
                }
            } catch (SQLException ex) {
                verErros.setErro(ex.getMessage());
            }

        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jbCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelarActionPerformed
            jbSalvar.setEnabled(false);
            jbCancelar.setEnabled(false);
            jbNovo.setEnabled(true);
            jbAlterar.setEnabled(false);
            jbExcluir.setEnabled(false);
            jTable1.setEnabled(true);
            campos();
            clear_campos();
            carrega_tabela();
    }//GEN-LAST:event_jbCancelarActionPerformed

    private void jbAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAlterarActionPerformed
        jbSalvar.setEnabled(true);
        jbCancelar.setEnabled(true);
        jbNovo.setEnabled(false);
        jbAlterar.setEnabled(false);
        jbExcluir.setEnabled(false);
        jTable1.setEnabled(false);
        campos();
    }//GEN-LAST:event_jbAlterarActionPerformed

    private void jbExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbExcluirActionPerformed
        if (JOptionPane.showOptionDialog(null, "Deseja remover esta Atividade Complementar?", "Removendo Atividade Complementar", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, JOopcoes, JOopcoes[0]) == JOptionPane.YES_OPTION) {
            try {
                conexao.stm.executeUpdate("delete from atividade_complementar where cod_aluno=" + jtfCod_acad.getText() + " and codigo=" + jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString() + " and cod_emp=" + index.cod_emp);

                jbSalvar.setEnabled(false);
                jbCancelar.setEnabled(false);
                jbNovo.setEnabled(true);
                jbAlterar.setEnabled(false);
                
                clear_campos();
                carrega_tabela();
            } catch (SQLException ex) {
                verErros.setErro(ex.getMessage());
            }
        }
    }//GEN-LAST:event_jbExcluirActionPerformed
    private void carrega_tabela(){
        int i = jTable1.getRowCount();
        int conta=0;
        String txtsql;
        javax.swing.table.DefaultTableModel jtm = (javax.swing.table.DefaultTableModel)jTable1.getModel();
        for(int x=(i-1);x>=0; --x ){
            jtm.removeRow(x);
        }
        try {
            conexao.Query("select * from atividade_complementar where cod_aluno="+jtfCod_acad.getText()+" and cod_emp="+index.cod_emp+" order by descricao");
            while (conexao.rs.next()) {
                conta+=conexao.rs.getInt("cg_hr_ativ_comp");
                jtm.addRow(new Object[]{conexao.rs.getString("codigo"),conexao.rs.getString("descricao"),conexao.rs.getString("nm_emp_ativ_comp"),conexao.rs.getString("cg_hr_ativ_comp")});
            }
            jtfTotal.setText(""+conta);
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    private void campos(){
        jtfCrg_hr.setEditable(!jtfDescricao.isEditable());
        jtfDt_fim.setEditable(!jtfDescricao.isEditable());
        jtfDt_ini.setEditable(!jtfDescricao.isEditable());
        jtfEmpresa.setEditable(!jtfDescricao.isEditable());
        jtfDescricao.setEditable(!jtfDescricao.isEditable());
    }

    private void clear_campos(){
        jtfCrg_hr.setText("");
        jtfDt_fim.setText("");
        jtfDt_ini.setText("");
        jtfEmpresa.setText("");
        jtfDescricao.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jbAlterar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbExcluir;
    private javax.swing.JButton jbNovo;
    private javax.swing.JButton jbPrint;
    private javax.swing.JButton jbSalvar;
    private javax.swing.JTextField jtfCod_acad;
    private javax.swing.JTextField jtfCrg_hr;
    private javax.swing.JTextField jtfDesc_acad;
    private javax.swing.JTextArea jtfDescricao;
    private javax.swing.JFormattedTextField jtfDt_fim;
    private javax.swing.JFormattedTextField jtfDt_ini;
    private javax.swing.JTextField jtfEmpresa;
    private javax.swing.JLabel jtfTotal;
    // End of variables declaration//GEN-END:variables

}
